package com.hoshing.test.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoshing.test.entity.TOrder;
import com.hoshing.test.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hskj
 * @since 2021-12-10
 */
@RestController
@RequestMapping("/test/order")
public class TOrderController {

    @Autowired
    ITOrderService orderService;

    @GetMapping("addOrder")
    public String addOrder(@RequestParam("remark") String remark){
        TOrder order = new TOrder();
        order.setRemark(remark);
        orderService.save(order);
        return "添加成功";
    }
    @GetMapping("getOrder")
    public String getOrder(@RequestParam("id") String id){
        //对传入id类型要求严格，什么类型就必须转型为什么类型
        return JSONObject.toJSONString(orderService.getById(Long.parseLong(id)));
    }
    @GetMapping("delOrder")
    public String delOrder(@RequestParam("id") String id){
        //对传入id类型要求严格，什么类型就必须转型为什么类型
        return JSONObject.toJSONString(orderService.removeById(Long.parseLong(id)));
    }

    @GetMapping("getAll")
    public String getAll(){
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_id");
        List<TOrder> datas = orderService.list(queryWrapper);
        StringBuilder sb = new StringBuilder();
        datas.forEach(data -> {
            sb.append(JSONObject.toJSONString(data));
            sb.append("</br>");
            System.out.println(JSONObject.toJSONString(data));
        });
        return sb.toString();
    }
    @GetMapping("getAllInfo")
    public String getAllInfo(){
        List<Map<String, Object>> datas = orderService.getAllInfoList();
        StringBuilder sb = new StringBuilder();
        datas.forEach(data -> {
            sb.append(JSONObject.toJSONString(data));
            sb.append("</br>");
            System.out.println(JSONObject.toJSONString(data));
        });
        return sb.toString();
    }
}

